/**
 * Created by leman on 2017/7/11.
 */
'use strict';
salesCtrl.controller('generateBillRCtrl', ['$rootScope', '$scope', '$http', '$location', '$stateParams',function ($rootScope, $scope, $http, $location, $stateParams) {
    //初始化第一页
    getPage($scope.p_current,$scope.p_pernum,function(){
        //alert("我是第一次加载");
    });
    function getPage(page,size,callback){
        // console.log($stateParams.data)
        $http.post("receivable/temp-list",{
            "versionNo":$stateParams.data
        }).success(function(res){
            // $scope.count=res.data.page.totalRecord;
            $scope.data=res.data;
            // console.log(res.data.cost.versionNo)
            // $scope.p_current = page;
            $scope.p_all_page =Math.ceil($scope.count/$scope.p_pernum);
            callback();
            /* }else{
             alert("加载失败");
             }  */
        });
    }
    //单选按钮选中
    $scope.select= function(id){
        //alert(id);
    };

    // 移除
    $scope.delBtn = function () {
        var value = this.x.id;
        // console.log(value);
        layer.confirm('是否确认移除？', {
                title: '提示'
                , btn: ['确认', '取消'] //按钮
            }, function () {
                $http.post("receivable/delete-detl-temp", {
                    "id": value
                }).success(function (res) {
                    if (res.success) {
                        getPage($scope.p_current, $scope.p_pernum, function () {});
                        layer.msg('已成功移除', {
                            icon: 1
                        });
                    } else {
                        getPage($scope.p_current, $scope.p_pernum, function () {});
                        layer.msg('移除失败', {
                            icon: 1
                        });
                    }
                });
        })
    };

    //调整金额
    $scope.adjustBtn=function(){
        var id = this.x.id;
        var comment = this.x.comment == null ? "" : this.x.comment;
        var sysCost=this.x.sysCost;//订单金额
        var settleCost=this.x.settleCost;//结算金额
        var alterCost=this.x.alterCost==null ? 0 : this.x.alterCost;//调整金额
        layer.open({
            type: 2,
            title:'调整金额',
            area: ['400px', '380px'], //宽高
            content:"index/adjust?sysCost="+sysCost+"&settleCost="+settleCost+"&alterCost="+alterCost+"&comment="+comment,//传参数：订单价，调整金额，结算价
            btn:["确定","取消"],
            btnAlign: 'c', //按钮居中
            yes:function(index, layero){
                layer.close(index);
                //获取跳转页面的值：订单价，调整金额，结算价，调整页面
                var sysCost = $("#sysCost").val();
                var alterCost = $("#alterCost").val();
                var settleCost = $("#settleCost").val();
                var comment = $("#comment").val();
                // console.log(sysCost)
                // console.log(alterCost)
                // console.log(settleCost)
                // console.log(comment)
                // console.log(id)

                $http.post("receivable/update-temp",{
                    "sysCost":sysCost,
                    "alterCost":alterCost,
                    "settleCost":settleCost,
                    "comment":comment,
                    "id":id
                }).success(function(res){
                    if(res.success) {
                        getPage($scope.p_current, $scope.p_pernum, function () {});
                        layer.msg('修改成功！');
                    }else{
                        layer.msg('修改失败！');
                    }
                });
            }
        });
    };
    //获取时间
    $scope.getTime=function($event,str){
        layui.laydate({
            elem:$event.target,
            istime:true,
            istoday:true,
            format:'YYYY-MM-DD',
            choose:function(date){
                $scope[str]=date;
            }
        })
    };

    //是否开发票
    $scope.BillingOptions = [{
        id: 10,
        name: "不开票"
    },
        {
            id: 20,
            name: "开票"
        }
        // ,
        // {
        //     id: 30,
        //     name: "已开票"
        // }
    ];
}]);

